package com.ruoyi.order.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.order.domain.OrderNumUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.order.mapper.DetectOrderMapper;
import com.ruoyi.order.domain.DetectOrder;
import com.ruoyi.order.service.IDetectOrderService;

import javax.validation.Validator;

/**
 * 委托单信息Service业务层处理
 * 
 * @author Charlie
 * @date 2023-04-24
 */
@Service
public class DetectOrderServiceImpl implements IDetectOrderService 
{
    @Autowired
    private DetectOrderMapper detectOrderMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询委托单信息
     * 
     * @param sampleId 委托单信息主键
     * @return 委托单信息
     */
    @Override
    public DetectOrder selectDetectOrderBySampleId(String sampleId)
    {
        return detectOrderMapper.selectDetectOrderBySampleId(sampleId);
    }

    /**
     * 查询委托单信息列表
     * 
     * @param detectOrder 委托单信息
     * @return 委托单信息
     */
    @Override
    public List<DetectOrder> selectDetectOrderList(DetectOrder detectOrder)
    {
        return detectOrderMapper.selectDetectOrderList(detectOrder);
    }

    /**
     * 新增委托单信息
     * 
     * @param detectOrder 委托单信息
     * @return 结果
     */
    @Override
    public int insertDetectOrder(DetectOrder detectOrder)
    {
        return detectOrderMapper.insertDetectOrder(detectOrder);
    }

    /**
     * 修改委托单信息
     * 
     * @param detectOrder 委托单信息
     * @return 结果
     */
    @Override
    public int updateDetectOrder(DetectOrder detectOrder)
    {
        return detectOrderMapper.updateDetectOrder(detectOrder);
    }

    /**
     * 批量删除委托单信息
     * 
     * @param sampleIds 需要删除的委托单信息主键
     * @return 结果
     */
    @Override
    public int deleteDetectOrderBySampleIds(String[] sampleIds)
    {
        return detectOrderMapper.deleteDetectOrderBySampleIds(sampleIds);
    }

    /**
     * 删除委托单信息信息
     * 
     * @param sampleId 委托单信息主键
     * @return 结果
     */
    @Override
    public int deleteDetectOrderBySampleId(String sampleId)
    {
        return detectOrderMapper.deleteDetectOrderBySampleId(sampleId);
    }

    /**
     * 委托单信息导入
     * @param detectOrderList 委托单数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return
     */
    @Override
    public String importData(List<DetectOrder> detectOrderList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(detectOrderList) || detectOrderList.size() == 0) {
            throw new ServiceException("导入的委托单数据不能为空");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (DetectOrder detectOrder : detectOrderList) {
            try {
                // 验证是否存在这条委托单数据
                // 从数据库中搜索，sampleId是否重复
                DetectOrder d = detectOrderMapper.selectDetectOrderBySampleId(detectOrder.getSampleId());
                // sampleId未重复，d为空
                List<DetectOrder> list = detectOrderMapper.selectDetectOrderList(null);
                if (StringUtils.isNull(d)) {
                    // 给id赋值，一个id对应一个projectName
                    // sampleId未重复，但projectName有可能重复，所以要进行循环检索
                    // projectName是否重复的标识
                    boolean flag = false;
                    for (DetectOrder order : list) {
                        if (detectOrder.getProjectName().equals(order.getProjectName())) {
                            // 如果projectName重复，则沿用它的projectId
                            detectOrder.setId(order.getId());
                            flag = true;
                            break;
                        }
                    }
                    // projectName没有重复，插入一个新id
                    if (!flag) {
                        detectOrder.setId(OrderNumUtil.getOrderId());
                    }
                    BeanValidators.validateWithException(validator, detectOrder);
                    // 插入数据
                    detectOrderMapper.insertDetectOrder(detectOrder);
                    detectOrder.setCreateBy(operName);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据 " + detectOrder.getSampleId() + "导入成功");
                } else if (isUpdateSupport) {
                    // 是否支持更新
                    detectOrderMapper.updateDetectOrder(detectOrder);
                    BeanValidators.validateWithException(validator, detectOrder);
                    detectOrder.setUpdateBy(operName);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据 " + detectOrder.getSampleId() + "更新成功");
                } else {
                    // 插入数据库失败
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、数据 " + detectOrder.getSampleId() + "导入失败");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、数据 " + detectOrder.getSampleId() + "导入失败";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "导入失败，共" + failureNum + "条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "导入成功，共" + successNum + "条，数据如下：");
        }
        return successMsg.toString();
    }
}
